<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/10 13:28
 * Desc: 相册
 */
class Service_Api_Photo extends Contract_Api_Photo
{

    /**
     * 相册类型
     */
    const TYPE_ID = 6;

    public function query_condition(): array
    {
        $attrs   = array();
        $attrArr = DB::select('id', 'attrname', 'pid')
            ->from('photo_attr')
            ->where('isopen', '=', 1)
            ->execute()
            ->as_array();

        foreach ($attrArr as $item) {
            if (!$item['pid']) {
                //第一层级
                $attrs[] = $item;
            }
        }

        foreach ($attrs as &$attr) {
            foreach ($attrArr as $item) {
                if ($attr['id'] == $item['pid']) {
                    $attr['child'][] = $item;
                }
            }
        }

        return array(
            'sort' => array(
                array('id' => 0, 'title' => '综合排序'),
                array('id' => 3, 'title' => '点赞次数')
            ),
            'attr' => $attrs,
        );
    }

    public function get_list($params): array
    {
        //排序
        $sort = Arr::get($params, 'sort', '0');
        $sort = $sort ?: '0';
        //属性id集合
        $attr_ids = Arr::get($params, 'attr_ids', '');
        //目的地
        $dest_id = Arr::get($params, 'dest_id', '');
        //页码
        $page     = Arr::get($params, 'page', 1);
        $pagesize = Arr::get($params, 'pagesize', 10);
        //关键字
        $keyword = Arr::get($params, 'keyword', '');

        $data = array();

        //目的地
        if ($dest_id) {
            $data['kindlist'] = $dest_id;
        }

        // 关键字
        if ($keyword) {
            $data['keyword'] = $keyword;
        }

        // 属性
        if (!empty($attr_ids)) {
            $data['attrid'] = explode('_', $attr_ids);
        }

        //分页
        $offset         = St_Page::offset($page, $pagesize);
        $data['offset'] = $offset;

        //排序
        $data['order'] = $sort;

        //查询相册
        $data = Model_Photo::photo_list($data);
        foreach ($data as &$v) {
            $pic_num = DB::select()
                ->from('photo_picture')
                ->where('pid', '=', $v['id'])
                ->execute()
                ->count();

            $v['litpic']  = $this->img_url($v['litpic'], 220, 150);
            $v['pic_num'] = $pic_num;
            $v['show_num'] = $v['shownum'] ?: 0;

            unset($v['aid']);
            unset($v['webid']);
            unset($v['shownum']);
        }

        return $data;
    }

    public function get_detail($params): array
    {
        $id = $params['id'];

        $data = DB::select()
            ->from('photo')
            ->where('ishidden', '=', 0)
            ->and_where('id', '=', $id)
            ->execute()
            ->current();

        if (empty($data)) {
            throw new Exception('查询相册失败');
        }

        //点击率加一
        Product::update_click_rate($data['aid'], self::TYPE_ID);

        //picture
        $pic_list = Model_Photo::photo_picture($id);
        foreach ($pic_list as &$v) {

            //描述
            if (empty($v['description'])) {
                $v['description'] = $data['content'];
            }

            $v = array(
                'litpic'=>  $this->img_url($v['litpic']),
                'description' => $v['description'],
            );
        }

        return array(
            'pic_list' => $pic_list,
            'comment_num' => Model_Comment::get_comment_num($id, self::TYPE_ID),
            'show_num' => $data['shownum'] ?: 0,
            'litpic' => $this->img_url($data['litpic']),
            'description' => $data['description'],
            'title' => $data['title'],
            'id' => $data['id'],
            'content' => $data['content'],
        );
    }
}